package com.moli.lumpSugar.common.interceptor;//package com.moli.lumpSugar.common.interceptor;
//
//
//
//import com.moli.lumpSugar.common.exception.HttpErrorEnum;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.core.annotation.Order;
//import org.springframework.stereotype.Component;
//import org.springframework.web.servlet.HandlerInterceptor;
//import org.springframework.web.servlet.ModelAndView;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.util.Objects;
//import java.util.Optional;
//
///**
// * @program: LumpSugarChat
// * @description: Token拦截器
// * @author: zhangjt
// * @create: 2024-10-24 11:33
// **/
//@Component
//@Order(-2)
//public class TokenInterceptor implements HandlerInterceptor {
//
//    public static final String HEADER_AUTHORIZATION = "Authorization";
//    public static final String AUTHORIZATION_SCAN = "Bearer ";
//    public static final String UID = "uid";
//
////    @Autowired
////    private LoginService loginService;
//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
////        // 从请求头获取token
////        String token = getToken(request);
////        Long validUid = loginService.getValidUid(token);
////        if (Objects.isNull(validUid)) {
////            // 判断请求路径是否为公开路径
////            String[] split = request.getRequestURI().split("/");
////            if (split.length > 2 && "public".equals(split[3])){
////                return true;
////            }
////            HttpErrorEnum.ACCESS_DENIED.sendHttpError(response);
////            return false;
////        }
////        loginService.renewalTokenIfNecessary(token);
////        request.setAttribute(UID, validUid);
//        return true;
//    }
//
//    private String getToken(HttpServletRequest request) {
//        String token = request.getHeader(HEADER_AUTHORIZATION);
//        return Optional.ofNullable(token)
//                .filter(h -> h.startsWith(AUTHORIZATION_SCAN))
//                .map(h -> h.replaceFirst(AUTHORIZATION_SCAN, ""))
//                .orElse(null);
//    }
//
//    @Override
//    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
//        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
//    }
//
//    @Override
//    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
//        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
//    }
//
//}
